package com;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;

import redis.clients.jedis.Jedis;

public class SocketClientTest {
	
	
	public static void main(String[] args) throws Exception {
		SocketClient.mainSock();
	}
	public static void jedisGet() {
		Jedis jedis = new Jedis("127.0.0.1");
		String hwf = jedis.get("hwf");
		System.out.println(hwf);
	}
	
}

class SocketClient{
	public static void mainSock() throws Exception {
		int i = 0;
		while (i<100) {
			int timeOutMs = 1000;
			Socket socket = new Socket();;
			// ->@wjw_add
			socket.setReuseAddress(true);
			socket.setKeepAlive(true); // Will monitor the TCP connection is
			// valid
			socket.setTcpNoDelay(true); // Socket buffer Whetherclosed, to
			// ensure timely delivery of data
			socket.setSoLinger(true, 0); // Control calls close () method,
			// the underlying socket is closed
			// immediately
			// <-@wjw_add
			//socket.setSoTimeout(timeOutMs);
			
			socket.connect(new InetSocketAddress("127.0.0.1", 1212), timeOutMs);
			
			//PrintWriter pw = new PrintWriter(os);
			InputStream in = socket.getInputStream();
			// 2.得到socket读写流
			OutputStream os = socket.getOutputStream();
			// 3.利用流按照一定的操作，对socket进行读写操作
			String info = "get hwf "+(i++)+"\n";
			//String info = "*3\r\n$3\r\nSET\r\n$5\r\nmykey\r\n$7\r\nmyvalue\r\n";
			System.out.println();
			os.write(info.getBytes());
			os.flush();
			System.out.println("client "+info);
			//Thread.sleep(10);
			//socket.shutdownOutput();
			/*ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
			byte[] temp = new byte[1];
			int realLen = 0;
			while ((realLen = in.read(temp)) != -1) {
				byteArrayOut.write(temp, 0, realLen);
				System.out.print("Client receive msg:" + temp[0]);
				System.out.println();
				System.out.println(new String(temp));
				if(temp[0]=='\n'){
					break;
				}
			}*/
			/*byte[] recv = byteArrayOut.toByteArray();
			System.out.println("Client receive msg:" + new String(recv));*/
		}
	}
}
